package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"strconv"
)

type AiExtansion struct {
	Id               int64
	Extension_name   string //分机号码
	Extension_number string //分机号码
	Extension_pswd   string //分机密码
	Reg_state        string //REGED ,UNREG  ,分机发register的状态记录
	Seat_state       string //up,down,座席上下线
	Seat_status      string //idle,busy,座席忙闲
}

type AiSeat struct {
	Id               int64
	Agent_no         string //座席号码
	Agent_name       string //座席号码
	Agent_pass       string //座席密码
	Agent_externsion string //对应的分机号
}

type AiSeatGroup struct {
	Id         int64
	Group_name string
}
type AiSeatGroupMap struct {
	Id             int64
	Agent_group    int64
	Agent_group_id int64
}

func (a *AiExtansion) TableName() string {
	return "call_extension"
}

func (a *AiSeat) TableName() string {
	return "cc_agent"
}

func (a *AiSeatGroup) TableName() string {
	return "cc_agent_group"
}

func (a *AiSeatGroupMap) TableName() string {
	return "cc_agent_group_map"
}

func ExtansionAdd(p *AiExtansion) (int64, error) {
	return orm.NewOrm().Insert(p)
}

func SeatAdd(p *AiSeat) (int64, error) {
	return orm.NewOrm().Insert(p)
}

func SeatGroupGetList(page, pageSize int, filters ...interface{}) ([]*AiSeatGroup, int64) {
	offset := (page - 1) * pageSize
	list := make([]*AiSeatGroup, 0)
	query := orm.NewOrm().QueryTable("cc_agent_group")
	//过滤 条件。
	if len(filters) > 0 {
		fmt.Println(filters[0].(string), filters[1])
		for i := 0; i < len(filters); i += 2 {
			query = query.Filter(filters[i].(string), filters[i+1])
		}
	}
	total, _ := query.Count()
	_, err := query.OrderBy("-id").Limit(pageSize, offset).All(&list)
	if err != nil {
		ErrorLog(err)
	}
	return list, total
}

//获取列表
func ExtansionGetList(page, pageSize int, filters ...interface{}) ([]*AiExtansion, int64) {
	offset := (page - 1) * pageSize
	list := make([]*AiExtansion, 0)
	query := orm.NewOrm().QueryTable("call_extension")
	//过滤 条件。
	if len(filters) > 0 {
		fmt.Println(filters[0].(string), filters[1])
		for i := 0; i < len(filters); i += 2 {
			query = query.Filter(filters[i].(string), filters[i+1])
		}
	}
	total, _ := query.Count()
	_, err := query.OrderBy("-id").Limit(pageSize, offset).All(&list)
	if err != nil {
		ErrorLog(err)
	}
	return list, total
}

//获取列表
func SeatGetList(page, pageSize int, gId int64) ([]AiSeat, int64) {
	var list []AiSeat
	limit := " "
	if page == 0 && pageSize == 0 {
	} else {
		limit = " ORDER BY id DESC LIMIT " + strconv.Itoa(pageSize) + " OFFSET " + strconv.Itoa((page-1)*pageSize)
	}
	sqlStr := "select a.id,a.agent_name " +
		"FROM cc_agent a,cc_agent_group g,cc_agent_group_map m " +
		"WHERE a.id=m.agent_id and g.id=m.agent_group_id and g.id="+strconv.FormatInt(gId,10)+limit
	_, err := orm.NewOrm().Raw(sqlStr).QueryRows(&list)
	if err != nil {
		ErrorLog(err)
		return []AiSeat{}, 0
	}
	sqlcountStr:="select count(1) " +
		"FROM cc_agent a,cc_agent_group g,cc_agent_group_map m " +
		"WHERE a.id=m.agent_id and g.id=m.agent_group_id and g.id="+strconv.FormatInt(gId,10)
	var count int64
	err = orm.NewOrm().Raw(sqlcountStr).QueryRow(&count)
	if err != nil {
		ErrorLog(err)
		return []AiSeat{}, 0
	}

	return list, count
}

//根据ID查询
func ExtansionGetById(id int) (*AiExtansion, error) {
	r := new(AiExtansion)
	err := orm.NewOrm().QueryTable("call_extension").Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}

//更新数据库
func (a *AiExtansion) Update(fields ...string) error {
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}

//删除数据库
func (a *AiExtansion) Delete(fields ...string) error {
	if _, err := orm.NewOrm().Delete(a, fields...); err != nil {
		return err
	}
	return nil
}

//根据ID查询
func SeatGetById(id int) (*AiSeat, error) {
	r := new(AiSeat)
	err := orm.NewOrm().QueryTable("cc_agent").Filter("id", id).One(r)
	if err != nil {
		return nil, err
	}
	return r, nil
}

//更新数据库
func (a *AiSeat) Update(fields ...string) error {
	if _, err := orm.NewOrm().Update(a, fields...); err != nil {
		return err
	}
	return nil
}

//删除数据库
func (a *AiSeat) Delete(fields ...string) error {
	if _, err := orm.NewOrm().Delete(a, fields...); err != nil {
		return err
	}
	return nil
}
